package com.ms.user.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.ms.user.fegin.FeginServer;
import com.ms.user.util.ImageCaptchaUtil;
import com.ms.user.util.OssUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.ms.user.entity.UserEntity;
import com.ms.user.service.UserService;
import com.ms.common.utils.PageUtils;
import com.ms.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 这个是用户表
 *
 * @author wc
 * @email sunlightcs@gmail.com
 * @date 2022-02-22 11:50:25
 */
@RestController
@RequestMapping("user/user")
public class UserController {

    @Autowired
    private FeginServer feginServer;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private OssUtils ossUtils;

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     *图片验证码  用于登陆  以及帮砍的应用
     */
    @GetMapping("/getYzm")
    public R getYzm(){
        //获取生成图片验证码的路径和code
        HashMap<String, String> imageCode = ImageCaptchaUtil.getImageCode();
        String url = imageCode.get("url");
        String code = imageCode.get("code");
        //将获取到的code存入redis中
        stringRedisTemplate.opsForValue().set("code",code,1, TimeUnit.HOURS);
        return R.ok().put("url",url);
    }


    /**
     * 手机号获取验证码
     */
    @RequestMapping("/phoneToYzm/{phone}")
    public R phoneToYzm(@PathVariable("phone") String phone){
        //验证手机号是否已注册
       R r =  userService.que(phone);
       if (r.getCode()==0){
           rabbitTemplate.convertAndSend("ms.shop","add.user.key",phone+"");
           return R.ok();
       }
       return r;
    }

    @RequestMapping("/feginGetName")
    public String feginGetName(@RequestParam Long uid){
        return userService.feginGetName(uid);
    }

    @RequestMapping("/upload")
    public R upload(MultipartFile file){
        //上传头像
        R r = ossUtils.uploadFile(file);
        return r;
    }


    /**
     * 注册  register
     */
    @RequestMapping("/zhuche")
    public R zhuche(@RequestBody UserEntity userEntity){
        System.out.println(userEntity);
        R r = userService.sa(userEntity);
        return r;
    }


    /**
     * login
     */
    @RequestMapping("/login")
    public R login(@RequestBody UserEntity userEntity,@RequestParam("uuid") String uuid){
        System.out.println(uuid);
        System.out.println(userEntity);
        String code = stringRedisTemplate.opsForValue().get("code");
        if (userEntity.getImgYzm()!=null&&code.equals(userEntity.getImgYzm())){
//            //判断试试是帮别人砍价
//            if (uuid.equals("undefined")){
                //uuid 没有值  不是帮砍 执行正常业务
                R r = userService.login(userEntity);
                return r;
//            }else{
//                R r = userService.login(userEntity);
//                // 属于帮砍  执行正常业务的同时 帮砍 生成帮砍记录   修改在redis中的商品价格
//                Long uid = Long.valueOf(String.valueOf(r.get("uid")));
//                R kanjia = feginServer.kanjia(uuid,uid);
//                System.out.println(kanjia);
//                if (kanjia.getCode().equals(0) && r.getCode().equals(0)){
//                    return r;
//                }
//                return R.error("助力错误");
//            }
        }
        return R.error("验证码错误");
    }

    @RequestMapping("/bklogin")
    public R login(@RequestParam("uuid") String uuid,@RequestParam("uid") String uid){
      //   属于帮砍  执行正常业务的同时 帮砍 生成帮砍记录   修改在redis中的商品价格
                Long uidd = Long.valueOf(uid);
                R kanjia = feginServer.kanjia(uuid,uidd);
                System.out.println(kanjia);
                if (kanjia.getCode().equals(0)){
                    return R.ok();
                }
                return R.error("助力错误");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }



}
